//
//  YLRelevantAgreementViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/22.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLRelevantAgreementViewController.h"
#import "YLRelevantAgreemenCell.h"
#import "YLRelevantAgreementViewModel.h"
#import "YLRelevantAgreementItem.h"
#import "YLPdfViewController.h"
#import <MJRefresh.h>
@interface YLRelevantAgreementViewController ()
@property (nonatomic, strong) YLRelevantAgreementViewModel *viewModel;
@end

@implementation YLRelevantAgreementViewController

- (YLRelevantAgreementViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLRelevantAgreementViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"相关协议";
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLRelevantAgreemenCell class]) bundle:nil] forCellReuseIdentifier:[YLRelevantAgreemenCell cellID]];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    __weak typeof(self) weakSelf = self;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [[weakSelf.viewModel.requestCommand execute:_productCode] subscribeNext:^(id  _Nullable x) {
            [weakSelf setEmpty];
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
    }];
    
    [self.viewModel.wh_loadDataErrorSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf setEmpty];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    [weakSelf.tableView.mj_header beginRefreshing];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLRelevantAgreemenCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLRelevantAgreemenCell cellID]];
    cell.model = self.viewModel.models[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLRelevantAgreementModel *model = self.viewModel.models[indexPath.section];
    YLPdfViewController *pdfCon = [[YLPdfViewController alloc] init];
    pdfCon.fileName = model.agreementTemp;
    
    [self.navigationController pushViewController:pdfCon animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString * text = @"抱歉,暂时还没有相关协议!";
    
    NSDictionary * attributes = @ { NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f ],
        NSForegroundColorAttributeName:YLColor(130, 130, 130)};
    
    return [[ NSAttributedString  alloc ] initWithString:text attributes:attributes];
}
- (void)dealloc
{
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}







@end
